/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seso;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.util.Calendar;

/**
 *
 * @author Administrator
 */
public class RCalendarField extends RTextField {
    private RCalendar myCalendar;
    private Image imageRover = Util.getUserImage("resources/CalendarRover.png");
    private Image imageIcon = Util.getUserImage("resources/CalendarIcon.png");
    private int drawX, drawY, drawWidth, drawHeight;
    private boolean allowSetText = false;
    private boolean isCalendarAdded = false;
    private static String DEFAULT_TEXT = "DDMMYY";

    public RCalendarField() {
        //this.setDocument(new IntegerDocument());
        this.setEnabled(false);
        this.setText(DEFAULT_TEXT);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                MouseClicked(evt);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        
        if (!java.beans.Beans.isDesignTime()) {
            if (!isCalendarAdded)
            {
                myCalendar = new RCalendar();
                //myCalendar.setBounds(this.getX(), this.getY() + this.getHeight(), 200, 130);
                myCalendar.setBounds(this.getX(), this.getY() + this.getHeight(), 150, 175);
                myCalendar.setVisible(false);
                if (this.getText().length() > 0 && !this.getText().equals(DEFAULT_TEXT)) {
                    Calendar calendar = Calendar.getInstance();
                    int day = Integer.parseInt(this.getText().substring(0, 2));
                    int month = Integer.parseInt(this.getText().substring(2, 4)) - 1;
                    int year = Integer.parseInt(Integer.toString(calendar.get(Calendar.YEAR)).substring(0, 2) + this.getText().substring(4));
                    calendar.set(Calendar.DATE, day);
                    calendar.set(Calendar.MONTH, month);
                    calendar.set(Calendar.YEAR, year);
                    myCalendar.setCalendar(calendar);
                }
                this.getParent().add(myCalendar, 0);
                isCalendarAdded = true;
            }

            if (myCalendar == null) {
                //myCalendar = new RCalendar1(this.getX(), this.getY() + this.getHeight());
                //RCalendar myCalendar = new RCalendar();
                //myCalendar.init();
                //myCalendar.setStrDate("210812");
                //myCalendar.updateView();
                //((JDialog)this.getParent()).getContentPane().add(myCalendar);
                //this.getParent().add(myCalendar);       
                //myCalendar.setVisible(false);
            }
            
            //myCalendar.setVisible(isMouseClicked);
            //myCalendar.repaint();
            //isMouseClicked = false;
            //if (allowSetText && myCalendar.isVisible()) 
            //if (allowSetText && myCalendar.isDateChanged()) 
            if (allowSetText && myCalendar.isDateChanged()) {
                this.setText(myCalendar.getSelectedDate());
            }
            else {
                this.setText(this.getText());
            }
            //this.setText(Integer.toString(myCalendar.getDay()));
            //this.setText(myCalendar.mouse);
            //this.setText("Hello");
            
            //myCalendar.setVisible(true);
            //myCalendar.show();
        }        
        
        drawX = getWidth() - drawWidth - 6;
        drawY = 5;
        drawWidth = getHeight() - drawY * 2;
        drawHeight = getHeight() - drawY * 2;
        
        setMargin(new Insets(1, 4, 1, drawWidth + drawY * 2));        
        g.drawImage(imageRover, drawX, drawY, drawWidth, drawHeight, this);
    }
    
    private void MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        //this.setText("121212");
        //javax.swing.JOptionPane.showMessageDialog(null, "Clicked.");
        myCalendar.setVisible(!myCalendar.isVisible());
        
        if (myCalendar.isVisible() && this.getText() == " ") {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, Integer.parseInt(this.getText().substring(1, 2)));
            myCalendar.setCalendar(null);
        }

        //myCalendar.init();
        //myCalendar = null;
        //isMouseClicked = true;
        //this.repaint();
        //myCalendar.setVisible(true);
        //myCalendar.resize(myCalendar.getHeight() + 10, myCalendar.getWidth() + 10);
        //myCalendar.repaint();
        //myCalendar.repaint();
        //this.setText(myCalendar.getStrDate());
        //this.setText(evt.getComponent().toString());
    }

    public boolean isAllowSetText() {
        return allowSetText;
    }

    public void setAllowSetText(boolean eetTextAllowed) {
        this.allowSetText = eetTextAllowed;
    }
}
